import { useFetch, useNuxtApp } from "#app";

import { Message } from "@arco-design/web-vue";

const baseUrl = "http://43.135.27.248/api";

export const getHomeArticle = (params) => {
	const url = baseUrl+'/home/index'
	return postFetch(url, params)
}

export const Login = (params) => {
	const url = baseUrl+'/user/login'
	return postFetch(url, params)
}

export const getUserInfo = (params) => {
	const url = baseUrl+'/user/getUserInfo'
	return postFetch(url, params)
}

export const postFetch = (url, params) => {
	const data = useFetch(url, {
		parseResponse: JSON.parse,
		baseURL: baseUrl,
		method: 'POST',
		watch: false,
		body: (params)
	}).then(r => {
		if (!r) {
			return {};
		}
		if (!r.data || !r.data.value) {
			return {};
		}

		let value = r.data.value
		if (value.code && value.code == 1) {
			return value
		} else {
			Message.error(value.msg)
			return {};
		}

	})
	return data;
}